package com.hehe.##project_name#.Controller;

import com.hehe.##project_name#.Model.ResponseEntity;
import com.hehe.##project_name#.Model.##ModelClassName#;
import com.hehe.##project_name#.Service.##ServiceClassName#;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.hehe.##project_name#.Util.ResponseUtil.handleErrorResponse;
import static com.hehe.##project_name#.Util.ResponseUtil.handleSuccessResponse;

@RequestMapping("/##className#")
@RestController()
@CrossOrigin(origins = "*", maxAge = 3600)
public class ##ControllerClassName# {
    private static final Logger LOGGER = LoggerFactory.getLogger(##ControllerClassName#.class);

    @Autowired
    private ##ServiceClassName# service;

    @PostMapping("/add##ModelClassName#")
    public ResponseEntity add(@RequestBody ##ModelClassName# obj) {
        try {
            return handleSuccessResponse(service.add(obj));
        } catch (Exception e) {
            return handleErrorResponse(e.getMessage());
        }
    }

    @PostMapping("/delete##ModelClassName#")
    public ResponseEntity delete(@RequestBody ##ModelClassName# obj) {
        try {
            if (service.getSingle(obj.getId()) == null) {
                throw new Exception("record not exist");
            }
            return handleSuccessResponse(service.delete(obj));
        } catch (Exception e) {
            return handleErrorResponse(e.getMessage());
        }
    }

    @PostMapping("/update##ModelClassName#")
    public ResponseEntity update(@RequestBody ##ModelClassName# obj) {
        try {
            if (service.getSingle(obj.getId()) == null) {
                throw new Exception("record not exist");
            }
            return handleSuccessResponse(service.update(obj));
        } catch (Exception e) {
            return handleErrorResponse(e.getMessage());
        }
    }

    @GetMapping("/getAll##ModelClassName#")
    public ResponseEntity getAll() {
        try {
            return handleSuccessResponse(service.getAll());
        } catch (Exception e) {
            return handleErrorResponse(e.getMessage());
        }
    }

    @GetMapping("/getSingle##ModelClassName#")
    public ResponseEntity getSingle(@RequestParam Integer id) {
        try {
            ##ModelClassName# obj = service.getSingle(id);
            if (null == obj) {
                throw new Exception("record not exist");
            }
            return handleSuccessResponse(obj);
        } catch (Exception e) {
            return handleErrorResponse(e.getMessage());
        }
    }
}